/*
 * Copyright (C) 2011 Emmanuel Tourdot
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 * $Id$
 */
package org.etourdot.xinclude;

import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * @author Emmanuel Tourdot
 */
public class XIncProc
{
    private static final Logger LOG = LoggerFactory.getLogger(XIncProc.class);
    private final Options options;

    private XIncProc()
    {
        options = new Options();
        options.addOption("h", false, "Help");
        final Option output = OptionBuilder.hasArg().withArgName("file").withDescription("use output file").create("o");
        options.addOption(output);
    }

    private int execute(final String[] args, final InputStream stdin, final PrintStream stdout,
            final PrintStream stderr)
    {
        final GnuParser parser = new GnuParser();
        try {
            final CommandLine commandLine = parser.parse(options, args);
        }
        catch (final ParseException ex)
        {
            help(stderr);
            return 1;
        }
        return 0;
    }

    private void help(final PrintStream destination)
    {
        final HelpFormatter helpFormatter = new HelpFormatter();
        final PrintWriter printWriter = new PrintWriter(destination);
        try
        {
            helpFormatter.printHelp(printWriter, helpFormatter.getWidth(), "java -jar xincproc.jar", null, options,
                    helpFormatter.getLeftPadding(), helpFormatter.getDescPadding(), null, true);
        }
        finally
        {
            printWriter.flush();
            printWriter.close();
        }
    }

    public static void main(final String[] args)
    {
        System.exit(new XIncProc().execute(args, System.in, System.out, System.err));
    }
}
